<?PHP
 
class Admin_point_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','积分事务');
		$this->assign('menuid1','5');
		$this->assign('menuid2','2'); 
		//权限判断
		factory::module('admin')->authSys('trans');
		
	}

	  
	
	function index__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';  
			if(!isset($_POST['pointtype_id']))   $_POST['pointtype_id']  = '';
			if(!isset($_POST['b_date']))   $_POST['b_date']  = '';
			if(!isset($_POST['e_date']))   $_POST['e_date']  = '';
  	  #查询条件
			$sql_where = ' where 0=0 '.func::speedsearch($_POST['keywords'],array( 'a.remark','a.point','b.name','c.username','c.name' ));
			if($_POST['pointtype_id'] != ''){
				$sql_where .= ' and a.pointtype_id="'.$_POST['pointtype_id'].'" ';
			}
			if($_POST['b_date']!=''){
				$sql_where .= ' and a.created >= "'.$_POST['b_date'].' 00:00:00" ';	
			}
			if($_POST['e_date']!=''){
				$sql_where .= ' and a.created <= "'.$_POST['e_date'].' 23:59:59" ';	
			}
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_point` a
			left join t_pointtype b on a.pointtype_id=b.id
			left join t_user c on a.user_id=c.id 
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.*,
					abs(a.point) as point,
					b.name as pointtype_name,
					b.actions,
					c.username,
					c.name as user_name
				from `t_point` a
				left join t_pointtype b on a.pointtype_id=b.id
				left join t_user c on a.user_id=c.id 
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();
		}
	 	$this->assign('nav','积分事务');
		$db=factory::getDBO();
		$this->assign('pointtype',$db->loadassoclist('select * from `t_pointtype` '));
	}
	public function view__(){
		$_GET=format::addslash_deep($_GET);
		$id = $_GET['id'];
		$db = factory::getDBO();
		$rs = $db->loadassoc('
			select
				a.*,
				abs(a.point) as point,
				b.name as pointtype_name,
				b.actions,
				c.username,
				c.name as user_name,
				d.name as created_username
			from `t_point` a
			left join t_pointtype b on a.pointtype_id=b.id
			left join t_user c on a.user_id=c.id 
			left join t_user d on a.created_user_id=d.id 
			where  a.id="'.$id.'" 
		');
		if(!$rs){
			exit('操作失败！,没有查到信息！');
		} 
	 	$this->assign('rs',$rs); 
	}
	public function userbalance__(){
		$_GET=format::addslash_deep($_GET);
		$id = $_GET['id'];
		$db = factory::getDBO();
		$rs = $db->loadassoc('
			select
				a.id,
				a.name,
				a.username,
				a.balance,
				a.point
			from `t_user` a
			where  a.id="'.$id.'" 
		');
		if(!$rs){
			exit('操作失败！,没有查到信息！');
		} 
	 	$this->assign('rs',$rs); 
	}
	
	# 添加
	public function add__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));
			
			$m_point = factory::module('point');
			$db = factory::getDBO();
			$db->begin();
			//循环会员
			$ids = explode(',',$form['user_id']);
			

			if(is_array($ids) && count($ids)>0){
				foreach($ids as $v){
					$tmprs = $db->loadassoc('select * from t_user where id="'.$v.'" ');
					if( $tmprs['sendtype'] == 0 ){
						exit('【'.$tmprs['name'].'】不是田园卡会员，暂不支持添加积分事务');
					}
				}
			}
						
			
			
			if(is_array($ids) && count($ids)>0){
				foreach($ids as $v){
					if(!$m_point->add(intval($form['pointtype_id']),intval($v),abs($form['point']),$form['remark'],factory::module('admin')->getuserid(),func::get_client_ip())){
						exit($m_point->getError());
					}
				}
			}
			$db->commit();
			exit('{success:1,msg:"操作成功！"}');
		}
	 	$this->assign('nav','积分事务 - 添加'); 
		$db=factory::getDBO();
		$this->assign('pointtype',$db->loadassoclist('select * from `t_pointtype` '));
	}
	 
}
?>